package util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.catalina.realm.RealmBase;

import entidade.UrlShorter;
import entidade.Usuario;

public class UsuarioControler  {

	public static String entidade ="shorterBD";
	
	/*public boolean AlterarUsuario(UsuarioVO u) throws ErroException {
		Usuario m = new Usuario();
		    m.setId(u.getId());
		    
		    m.setPerfil("admin");
		    m.setNome(u.getNome());
		    m.setFone(u.getFone());
			m.setLogin(u.getLogin());
			m.setSenha(RealmBase.Digest(u.getSenha(), "MD5", "UTF-8"));
		
		  try{
			  EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		      manager.getTransaction().begin();
		 
				manager.merge(m);
		
		    manager.getTransaction().commit();
		    manager.close();
		    
		    return true;
		    
		  }catch (Exception e) {
				throw new ErroException();

			}
	
	}*/

	public boolean SalvarUsuario(Usuario m, String local) throws Exception {
		
		     	m.setSenha(RealmBase.Digest(m.getSenha(), "MD5", "UTF-8"));
				
				m.setDataCadastro(new Date());
				m.setPaisLingua(local);
				
			
			  try{
				EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
				manager.getTransaction().begin();
			
				manager.persist(m);
				
			    manager.getTransaction().commit();
			    manager.close();
			    return true;
			    
			  }catch (Exception e) {
					throw new Exception();

				}
		
	}
	public boolean AtualizarCadastroUsuario(Usuario m, boolean alteracaoSenha) throws Exception {
		
		
		
		if(alteracaoSenha)
     	   m.setSenha(RealmBase.Digest(m.getSenha(),"MD5", "UTF-8"));
		
		EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		
	  try{
		manager.getTransaction().begin();
		manager.merge(m);
	    return true;
	    
	  }catch (Exception e) {
			throw new Exception();

		} finally {  
			 manager.getTransaction().commit();
			 manager.close();
		 }  

}
	
	public boolean atualizaUsuario(Usuario u, UrlShorter url, String localCriado, boolean peronalizado) throws Exception {
	
	  try{
		  url.setDataCriacao(new Date());
		  url.setPaisCriacao(localCriado);
		 
		EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		manager.getTransaction().begin();
	
		manager.merge(u);
		
		if(peronalizado)
		   manager.persist(url);
		
		manager.merge(url);
		
		u.getUrlShorter().add(url);
		
		manager.merge(u);
		//manager.persist(u);
		
	    manager.getTransaction().commit();
	    manager.close();
	    return true;
	    
	  }catch (Exception e) {
			throw new Exception();

		}
	


}

	/*public UsuarioVO buscaUsuarioPeloID(Integer id)  throws ErroException {
		Usuario u = null;
		try {
			EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
			manager.getTransaction().begin();
			u = manager.find(Usuario.class, id);

			manager.getTransaction().commit();
			manager.close();

			if(u!=null){
				UsuarioVO vo = new UsuarioVO();
				vo.setId(u.getId());
				vo.setLogin(u.getLogin());
				vo.setNome(u.getNome());
				vo.setPerfil(u.getPerfil());
				vo.setSenha(u.getSenha());
				vo.setFone(u.getFone());
				return vo;
			}else
				return null;
			
		} catch (Exception e) {
			throw new ErroException();

		}
	}*/

	/*public List<UsuarioVO> buscaTodosUsuario() throws ErroException {
		try{
		    EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
			Query q = manager.createQuery("from Usuario u");
		
			List<UsuarioVO>  mVo= new ArrayList<UsuarioVO>(); 
			
			List<Usuario> resultList =q.getResultList();
			
			for(Usuario m : resultList){
				UsuarioVO vo = new UsuarioVO();
				vo.setId(m.getId());
				vo.setLogin(m.getLogin());
				vo.setNome(m.getNome());
				vo.setPerfil(m.getPerfil());
				vo.setSenha(m.getSenha());
				vo.setFone(m.getFone());
				
				mVo.add(vo);
			}
				
			return mVo;
			
		}catch (Exception e) {
			throw new ErroException();

		}
		
	}
*/
/*	public boolean excluirUsuario(UsuarioVO u) throws ErroException {
		Usuario m = new Usuario();
		
		try{
			 EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		     manager.getTransaction().begin();
		      m = manager.find(Usuario.class,u.getId());
		     
		     manager.merge(m);
		     manager.remove(m);
		   
		    manager.getTransaction().commit();
		    manager.close();
		    return true;
		  
		  }catch (Exception e) {
				throw new ErroException();

			}
		
		
	}*/

	public Usuario buscaUsuarioPeloLogin(String login, String senha) throws Exception {
		
		Usuario user = null;
				try {
					EntityManager manager2 = Persistence.createEntityManagerFactory(entidade).createEntityManager();

					Query use = manager2.createQuery("from Usuario u where u.login = :login");
					use.setParameter("login", login);
					
					if(use.getSingleResult()!=null){
					   user = (Usuario) use.getSingleResult();
						   if(user.getSenha().equals(RealmBase.Digest(senha,"MD5", "UTF-8")))//m.setSenha(RealmBase.Digest(m.getSenha(), "MD5", "UTF-8"));
						     return user;
					}else
						return null;
					
				} catch (Exception e) {
					throw new Exception();
				}
				return null;
	}
	
         public boolean VerificaLogin(String login) throws Exception {
		
        	 Usuario user = null;
				try {
					EntityManager manager2 = Persistence.createEntityManagerFactory(entidade).createEntityManager();

					Query use = manager2.createQuery("from Usuario u where u.login = :login");
					use.setParameter("login", login);
					
					if(use.getSingleResult()!=null){
							user = (Usuario) use.getSingleResult();
							if(user!=null){
								return true;
							}else
								return false;
					}else
						return false;
					
				} catch (Exception e) {
					throw new Exception();
				}
				
	}
	
	/*public List<UsuarioVO> buscaUsuarioPorNome(String nome) throws ErroException {
		 EntityManager manager = Persistence.createEntityManagerFactory(entidade).createEntityManager();
		 manager.getTransaction().begin();
		 Query query = manager.createQuery("SELECT a FROM Usuario a WHERE a.nome LIKE '"+nome+"%'");
		
		 List<Usuario> resultList = query.getResultList();
		 List<UsuarioVO> mVo = new ArrayList<UsuarioVO>();
		 
		 
		 for(Usuario m : resultList){
				UsuarioVO vo = new UsuarioVO();
				vo.setId(m.getId());
				vo.setLogin(m.getLogin());
				vo.setNome(m.getNome());
				vo.setPerfil(m.getPerfil());
				vo.setSenha(m.getSenha());
				vo.setFone(m.getFone());
				
				mVo.add(vo);
			}
				
			return mVo;
		
	}*/

	
}
